import { CHANGEINPUTVALUE,CHANGELIST,LISTDELETE,INITLISTACTION,GETINITLIST} from './actiontypes';
let defaultState = {
    inputValue: "",
    list: ['fdff','dfdsf','dfdf']
  };
//   reducer可以接收state，但不可以修改state
//  reducer要求是一个纯函数，给定固定的输入就有固定的输出
export default (state = defaultState, action) => {
    if(action.type === CHANGEINPUTVALUE){
        const newState = JSON.parse(JSON.stringify(state))
        newState.inputValue = action.value
        return newState
    } else if(action.type === CHANGELIST){
        const newState = JSON.parse(JSON.stringify(state))
        newState.list.push(newState.inputValue)
        newState.inputValue = ''
        return newState
    } else if(action.type === LISTDELETE){
        const newState = JSON.parse(JSON.stringify(state))
        newState.list.splice(action.index,1)
        return newState
    } else if(action.type ===INITLISTACTION){
      const newState = JSON.parse(JSON.stringify(state))
        newState.list = action.data
        return newState
    } else if(action.type === 'changeinputvalue'){
      const newState = JSON.parse(JSON.stringify(state))
      newState.inputValue = action.value
      return newState
    } else if(action.type === 'submitinpuvalue'){
      const newState = JSON.parse(JSON.stringify(state))
      newState.list.push(newState.inputValue)
      newState.inputValue = ''
      return newState
    } else if(action.type === 'deleteinputvalue'){
      const newState = JSON.parse(JSON.stringify(state))
      newState.list.splice(action.index,1)
      return newState
    }
  return state;
};
